package org.springblade.common.utils.delayed;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.PropertyValueConst;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.thierrysquirrel.annotation.MessageListener;
import com.github.thierrysquirrel.annotation.RocketListener;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.mq.MqMessageService;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.ElevatorStopPlanSimpleDTO;
import org.springblade.modules.admin.entity.*;
import org.springblade.modules.admin.entity.feishu.FeiShuElevatorStopCertContentTemplate;
import org.springblade.modules.admin.enums.*;
import org.springblade.modules.admin.service.*;
import org.springblade.modules.resource.utils.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: Jackey
 * Date: 2019/11/27
 * Time: 下午2:30
 * Description:
 */
@Slf4j
@RocketListener(groupID = "GID_STOP_ELEVATOR",messageModel = PropertyValueConst.BROADCASTING)
public class StopElevatorReceive {

	@Autowired
	private IMaintainPlanContentService planContentService;
	@Autowired
	private IMaintainPlanContentUserService planContentUserService;
	@Autowired
	private IElevatorStopLogService elevatorStopLogService;
	@Autowired
	private MqMessageService mqMessageService;

	private static final String FS_E_S_WEBHOOK_TEST = "https://open.feishu.cn/open-apis/bot/v2/hook/5757acc0-e716-4b85-8ed1-69d5f2313acc";
	private static final String FS_E_S_WEBHOOK_PROD = "https://open.feishu.cn/open-apis/bot/v2/hook/ec158f10-29c7-4bbd-a61e-4638ec801e59";

	/**
	 * 发送电梯停梯
	 * @param
	 * message {
	 *         msgType: 1-停梯时发送 2-签到时发送 3-维修人员第一次签到选择电梯状态正常 4-维修人员第一次签退选择电梯状态停梯
	 *         contentType: 1-一小时 2-两小时 3-四小时
	 *         planId: 工单id
	 *         elevatorId: 电梯id
	 *         initStatusName: 电梯初始运行状态
	 *         setStatusName: 电梯变更运行状态
	 * }
	 * @return
	 */
//	@MessageListener(topic = "stop_elevator", tag = "elevator_stop_test")
	public void sendElevatorStopTestMsg(String message) {
		JSONObject jsonObject = JSONObject.parseObject(message);
		log.info("===============接收队列接收消息====================");
		log.info("接收时间:[{}],接受内容:[{}]", LocalDateTime.now(), message);

		if(Func.isEmpty(jsonObject.getLongValue("elevatorId")) || Func.isEmpty(jsonObject.getLongValue("planId"))){
			return;
		}
		int msgType = jsonObject.getIntValue("msgType");
		int contentType = jsonObject.getIntValue("contentType");
		Long elevatorId = jsonObject.getLongValue("elevatorId");
		Long planId = jsonObject.getLongValue("planId");
		ElevatorStopPlanSimpleDTO simpleDTO = planContentService.selectElevatorStopPlanSimple(planId);
		ElevatorStopLog elevatorStopLog = elevatorStopLogService.getLatestLog(elevatorId);
		long between;
		if(msgType == 1){
			between = DateUtil.between(elevatorStopLog.getCreateTime(),DateUtil.date(), DateUnit.MS);
			log.info("工单状态:[{}],签到统计:[{}],停梯时长:[{}]", simpleDTO.getPlanStatus(), simpleDTO.getSignCount(),between);
			if(simpleDTO.getPlanStatus().equals(TaskStatusEnum.WX_TO_START.getStatus()) && simpleDTO.getSignCount() == 0 && between >= 5*60*1000){
//			if(simpleDTO.getPlanStatus().equals(TaskStatusEnum.WX_TO_START.getStatus()) && simpleDTO.getSignCount() == 0 && between >= 60*60*1000){
				if(contentType != 3){
					FeiShuElevatorStopCertContentTemplate template = new FeiShuElevatorStopCertContentTemplate(planId,msgType,contentType,contentType,simpleDTO);
					Map<String,Object> map = new HashMap<>();
					map.put("msg_type","interactive");
					map.put("card",template.getCertContentTemplate());
					String result = HttpUtil.post(FS_E_S_WEBHOOK_TEST,JSONObject.toJSONString(map));
					log.info("请求时间:[{}],请求结果:[{}]", LocalDateTime.now(), result);

					//继续发送停梯延时消息
					long sendTime;
					JSONObject msg = new JSONObject();
					if(contentType == 1){
						sendTime = DateUtil.date().getTime() + 5*60*1000;
//						sendTime = DateUtil.date().getTime() + 60*60*1000;
						msg.put("contentType",2);
					}else {
						sendTime = DateUtil.date().getTime() + 5*60*1000;
//						sendTime = DateUtil.date().getTime() + 120*60*1000;
						msg.put("contentType",3);
					}
					msg.put("msgType",1);
					msg.put("planId",planId);
					msg.put("elevatorId",elevatorId);
					mqMessageService.sendElevatorStopMsg(sendTime,JSONObject.toJSONString(msg));
				}else {
					FeiShuElevatorStopCertContentTemplate template = new FeiShuElevatorStopCertContentTemplate(planId,msgType,contentType,4,simpleDTO);
					Map<String,Object> map = new HashMap<>();
					map.put("msg_type","interactive");
					map.put("card",template.getCertContentTemplate());
					String result = HttpUtil.post(FS_E_S_WEBHOOK_TEST,JSONObject.toJSONString(map));
					log.info("请求时间:[{}],请求结果:[{}]", LocalDateTime.now(), result);
				}
			}
		}
		if(msgType == 2){
			long stopHour = DateUtil.between(elevatorStopLog.getCreateTime(),DateUtil.date(), DateUnit.HOUR);
			between = DateUtil.between(simpleDTO.getFirstSignTime(),DateUtil.date(), DateUnit.MS);
			log.info("工单状态:[{}],签退统计:[{}],停梯时长:[{}]", simpleDTO.getPlanStatus(), simpleDTO.getSignCount(),between);
			if(simpleDTO.getPlanStatus().equals(TaskStatusEnum.WX_UNDER_WAY.getStatus()) && simpleDTO.getSignOutCount() == 0 && between >= 5*60*1000){
//			if(simpleDTO.getPlanStatus().equals(TaskStatusEnum.WX_TO_START.getStatus()) && simpleDTO.getSignOutCount() == 0 && between >= 120*60*1000){
				if(contentType != 2){
					FeiShuElevatorStopCertContentTemplate template = new FeiShuElevatorStopCertContentTemplate(planId,msgType,contentType, (int) stopHour,simpleDTO);
					Map<String,Object> map = new HashMap<>();
					map.put("msg_type","interactive");
					map.put("card",template.getCertContentTemplate());
					String result = HttpUtil.post(FS_E_S_WEBHOOK_TEST,JSONObject.toJSONString(map));
					log.info("请求时间:[{}],请求结果:[{}]", LocalDateTime.now(), result);

					//继续发送停梯延时消息
					long sendTime = DateUtil.date().getTime() + 5*60*1000;
//					long sendTime = DateUtil.date().getTime() + 120*60*1000;
					JSONObject msg = new JSONObject();
					msg.put("contentType",2);
					msg.put("msgType",2);
					msg.put("planId",planId);
					msg.put("elevatorId",elevatorId);
					mqMessageService.sendElevatorStopMsg(sendTime,JSONObject.toJSONString(msg));
				}else {
					FeiShuElevatorStopCertContentTemplate template = new FeiShuElevatorStopCertContentTemplate(planId,msgType,contentType,(int) stopHour,simpleDTO);
					Map<String,Object> map = new HashMap<>();
					map.put("msg_type","interactive");
					map.put("card",template.getCertContentTemplate());
					String result = HttpUtil.post(FS_E_S_WEBHOOK_TEST,JSONObject.toJSONString(map));
					log.info("请求时间:[{}],请求结果:[{}]", LocalDateTime.now(), result);
				}
			}
		}
		if(msgType == 3){
			FeiShuElevatorStopCertContentTemplate template = new FeiShuElevatorStopCertContentTemplate(planId,msgType,null, null,simpleDTO);
			Map<String,Object> map = new HashMap<>();
			map.put("msg_type","interactive");
			map.put("card",template.getCertContentTemplate());
			String result = HttpUtil.post(FS_E_S_WEBHOOK_TEST,JSONObject.toJSONString(map));
			log.info("请求时间:[{}],请求结果:[{}]", LocalDateTime.now(), result);
		}
		if(msgType == 4){
			simpleDTO.setInitStatusName(jsonObject.getString("initStatusName"));
			simpleDTO.setSetStatusName(jsonObject.getString("setStatusName"));
			FeiShuElevatorStopCertContentTemplate template = new FeiShuElevatorStopCertContentTemplate(planId,msgType,null, null,simpleDTO);
			Map<String,Object> map = new HashMap<>();
			map.put("msg_type","interactive");
			map.put("card",template.getCertContentTemplate());
			String result = HttpUtil.post(FS_E_S_WEBHOOK_TEST,JSONObject.toJSONString(map));
			log.info("请求时间:[{}],请求结果:[{}]", LocalDateTime.now(), result);
		}
	}

//	@MessageListener(topic = "stop_elevator", tag = "elevator_stop || elevator_stop_test")
	public void sendMsg(String message) {
		JSONObject jsonObject = JSONObject.parseObject(message);
		if(jsonObject.getString("sendMsgType").equals("elevator_stop")){
			this.sendElevatorStopMsg(message);
		}else if(jsonObject.getString("sendMsgType").equals("elevator_stop_test")){
			this.sendElevatorStopTestMsg(message);
		}
	}

	/**
	 * 发送电梯停梯
	 * @param
	 * message {
	 *         msgType: 1-停梯时发送 2-签到时发送 3-维修人员第一次签到选择电梯状态正常 4-维修人员第一次签退选择电梯状态停梯
	 *         contentType: 1-一小时 2-两小时 3-四小时
	 *         planId: 工单id
	 *         elevatorId: 电梯id
	 *         initStatusName: 电梯初始运行状态
	 *         setStatusName: 电梯变更运行状态
	 * }
	 * @return
	 */
	@MessageListener(topic = "stop_elevator", tag = "elevator_stop")
	public void sendElevatorStopMsg(String message) {
		JSONObject jsonObject = JSONObject.parseObject(message);
		log.info("===============接收队列接收消息====================");
		log.info("接收时间:[{}],接受内容:[{}]", LocalDateTime.now(), message);

		if(Func.isEmpty(jsonObject.getLongValue("elevatorId")) || Func.isEmpty(jsonObject.getLongValue("planId"))){
			return;
		}
		int msgType = jsonObject.getIntValue("msgType");
		int contentType = jsonObject.getIntValue("contentType");
		Long elevatorId = jsonObject.getLongValue("elevatorId");
		Long planId = jsonObject.getLongValue("planId");
		ElevatorStopPlanSimpleDTO simpleDTO = planContentService.selectElevatorStopPlanSimple(planId);
		ElevatorStopLog elevatorStopLog = elevatorStopLogService.getLatestLog(elevatorId);
		long between;
		if(msgType == 1){
			between = DateUtil.between(elevatorStopLog.getCreateTime(),DateUtil.date(), DateUnit.MS);
//			if(simpleDTO.getPlanStatus().equals(TaskStatusEnum.WX_TO_START.getStatus()) && simpleDTO.getSignCount() == 0 && between >= 1*60*1000){
			if(simpleDTO.getPlanStatus().equals(TaskStatusEnum.WX_TO_START.getStatus()) && simpleDTO.getSignCount() == 0 && between >= 60*60*1000){
				if(contentType != 3){
					FeiShuElevatorStopCertContentTemplate template = new FeiShuElevatorStopCertContentTemplate(planId,msgType,contentType,contentType,simpleDTO);
					Map<String,Object> map = new HashMap<>();
					map.put("msg_type","interactive");
					map.put("card",template.getCertContentTemplate());
					String result = HttpUtil.post(FS_E_S_WEBHOOK_PROD,JSONObject.toJSONString(map));
					log.info("请求时间:[{}],请求结果:[{}]", LocalDateTime.now(), result);

					//继续发送停梯延时消息
					long sendTime;
					JSONObject msg = new JSONObject();
					if(contentType == 1){
//						sendTime = DateUtil.date().getTime() + 1*60*1000;
						sendTime = DateUtil.date().getTime() + 60*60*1000;
						msg.put("contentType",2);
					}else {
//						sendTime = DateUtil.date().getTime() + 1*60*1000;
						sendTime = DateUtil.date().getTime() + 120*60*1000;
						msg.put("contentType",3);
					}
					msg.put("msgType",1);
					msg.put("planId",planId);
					msg.put("elevatorId",elevatorId);
					mqMessageService.sendElevatorStopMsg(sendTime,JSONObject.toJSONString(msg));
				}else {
					FeiShuElevatorStopCertContentTemplate template = new FeiShuElevatorStopCertContentTemplate(planId,msgType,contentType,4,simpleDTO);
					Map<String,Object> map = new HashMap<>();
					map.put("msg_type","interactive");
					map.put("card",template.getCertContentTemplate());
					String result = HttpUtil.post(FS_E_S_WEBHOOK_PROD,JSONObject.toJSONString(map));
					log.info("请求时间:[{}],请求结果:[{}]", LocalDateTime.now(), result);
				}
			}
		}
		if(msgType == 2){
			long stopHour = DateUtil.between(elevatorStopLog.getCreateTime(),DateUtil.date(), DateUnit.HOUR);
			between = DateUtil.between(simpleDTO.getFirstSignTime(),DateUtil.date(), DateUnit.MS);
//			if(simpleDTO.getPlanStatus().equals(TaskStatusEnum.WX_TO_START.getStatus()) && simpleDTO.getSignOutCount() == 0 && between >= 1*60*1000){
			if(simpleDTO.getPlanStatus().equals(TaskStatusEnum.WX_TO_START.getStatus()) && simpleDTO.getSignOutCount() == 0 && between >= 120*60*1000){
				if(contentType != 2){
					FeiShuElevatorStopCertContentTemplate template = new FeiShuElevatorStopCertContentTemplate(planId,msgType,contentType, (int) stopHour,simpleDTO);
					Map<String,Object> map = new HashMap<>();
					map.put("msg_type","interactive");
					map.put("card",template.getCertContentTemplate());
					String result = HttpUtil.post(FS_E_S_WEBHOOK_PROD,JSONObject.toJSONString(map));
					log.info("请求时间:[{}],请求结果:[{}]", LocalDateTime.now(), result);

					//继续发送停梯延时消息
//					long sendTime = DateUtil.date().getTime() + 1*60*1000;
					long sendTime = DateUtil.date().getTime() + 120*60*1000;
					JSONObject msg = new JSONObject();
					msg.put("contentType",2);
					msg.put("msgType",2);
					msg.put("planId",planId);
					msg.put("elevatorId",elevatorId);
					mqMessageService.sendElevatorStopMsg(sendTime,JSONObject.toJSONString(msg));
				}else {
					FeiShuElevatorStopCertContentTemplate template = new FeiShuElevatorStopCertContentTemplate(planId,msgType,contentType,(int) stopHour,simpleDTO);
					Map<String,Object> map = new HashMap<>();
					map.put("msg_type","interactive");
					map.put("card",template.getCertContentTemplate());
					String result = HttpUtil.post(FS_E_S_WEBHOOK_PROD,JSONObject.toJSONString(map));
					log.info("请求时间:[{}],请求结果:[{}]", LocalDateTime.now(), result);
				}
			}
		}
		if(msgType == 3){
			FeiShuElevatorStopCertContentTemplate template = new FeiShuElevatorStopCertContentTemplate(planId,msgType,null, null,simpleDTO);
			Map<String,Object> map = new HashMap<>();
			map.put("msg_type","interactive");
			map.put("card",template.getCertContentTemplate());
			String result = HttpUtil.post(FS_E_S_WEBHOOK_PROD,JSONObject.toJSONString(map));
			log.info("请求时间:[{}],请求结果:[{}]", LocalDateTime.now(), result);
		}
		if(msgType == 4){
			simpleDTO.setInitStatusName(jsonObject.getString("initStatusName"));
			simpleDTO.setSetStatusName(jsonObject.getString("setStatusName"));
			FeiShuElevatorStopCertContentTemplate template = new FeiShuElevatorStopCertContentTemplate(planId,msgType,null, null,simpleDTO);
			Map<String,Object> map = new HashMap<>();
			map.put("msg_type","interactive");
			map.put("card",template.getCertContentTemplate());
			String result = HttpUtil.post(FS_E_S_WEBHOOK_PROD,JSONObject.toJSONString(map));
			log.info("请求时间:[{}],请求结果:[{}]", LocalDateTime.now(), result);
		}
	}

}


























